package luongvd.weather.model.adapter;

import java.util.ArrayList;

import luongvd.weather.R;
import luongvd.weather.common.Const;
import luongvd.weather.common.Utils;
import luongvd.weather.model.object.ObjectWeather;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterWeatherCity extends ArrayAdapter<ObjectWeather>{

	Activity activity;
	ArrayList<ObjectWeather> arrayWeather;
	
	public AdapterWeatherCity(Activity context, ArrayList<ObjectWeather> objects) {
		super(context, R.layout.item_list_city, objects);
		this.arrayWeather = objects;
		this.activity = context;
	}
	
	class ViewHolder{
		TextView name_city, date_city, tv_temp, tv_temp_max, tx_status;
		ImageView icon_weather;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null)
		{
			holder = new ViewHolder();
			LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = vi.inflate(R.layout.item_list_city, null);
			holder.name_city = (TextView)convertView.findViewById(R.id.name_city);
			holder.date_city = (TextView)convertView.findViewById(R.id.date_city);
			holder.tv_temp = (TextView)convertView.findViewById(R.id.tv_temp);
			holder.tv_temp_max = (TextView)convertView.findViewById(R.id.tv_temp_max);
			holder.tx_status = (TextView)convertView.findViewById(R.id.tx_status);
			holder.icon_weather = (ImageView)convertView.findViewById(R.id.icon_weather);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		ObjectWeather objectWeather = arrayWeather.get(position);
		if(objectWeather != null)
		{
			holder.name_city.setText(objectWeather.getName_city());
			holder.date_city.setText(objectWeather.getDate_city());
			holder.tv_temp.setText(objectWeather.getTemp()+Const.CELSIUS);
			if(objectWeather.getTemp_min().equals(objectWeather.getTemp_max()) == false){
				String temp_max_min = objectWeather.getTemp_min()+Const.CELSIUS+" - "+objectWeather.getTemp_max()+Const.CELSIUS;
				holder.tv_temp_max.setText(temp_max_min);
			}else{
				holder.tv_temp_max.setVisibility(View.GONE);
			}
			
			holder.tx_status.setText(objectWeather.getDescription_weather());
			String name_icon = objectWeather.getIcon_weather()+".png";
			holder.icon_weather.setImageBitmap(Utils.getBitmap(activity, name_icon));
		}
		
		return convertView;
	}

}
